package com.ceshiren.hogwarts.restassured.wechat.apiobject.framwork.global;

/**
 * @author Seven
 * @date 2020/12/26 15:07
 */

import com.ceshiren.hogwarts.restassured.wechat.apiobject.framwork.actiions.ApiActionModel;
import com.ceshiren.hogwarts.restassured.wechat.apiobject.framwork.api.ApiObjectModel;
import com.ceshiren.hogwarts.restassured.wechat.apiobject.framwork.test.Test02_ApiObjectModelTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.lang.reflect.Array;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 接口对象加载器
 */
public class ApiLoader {
    /**
     * 加载所有api，Object对象，并保存到本列表中
     */
    private static List<ApiObjectModel> apis = new ArrayList<>();
    public static final Logger logger = LoggerFactory.getLogger(Test02_ApiObjectModelTest.class);

    public static void load(String dir){
        Arrays.stream(new File(dir).list()).forEach(path->{
            try {
                //遍历每个yaml文件添加到apis里
                apis.add(ApiObjectModel.load(dir+"/"+path));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }


    public static ApiActionModel getAction(String apiName,String actionName){
        final ApiActionModel[] apiActionModel = {new ApiActionModel()};
        apis.stream().filter(api -> api.getName().equals(apiName)).forEach(api ->{
            apiActionModel[0] = api.getActions().get(actionName);
        });

        if(apiActionModel[0] !=null){
            return apiActionModel[0];
        }else {
            logger.info("没有找到接口对象: "+apiName+"中的action"+actionName);
        }
        return null;
    }
}
